import ErrorManager from "../ErrorManager";

const {ccclass, property} = cc._decorator;

@ccclass
export default class HallManager extends cc.Component {
    @property(cc.Node)
    loginPanel:cc.Node=null;
    @property(cc.Node)
    inputRoomPanel:cc.Node=null;
    @property(cc.Node)
    loadingNode:cc.Node=null;
    @property(cc.Node)
    regPanelBtn:cc.Node=null;
    @property(cc.Prefab)
    errorPrefab:cc.Prefab=null;
    @property(cc.Node)
    regPanel:cc.Node=null;


    roomLabel:cc.Label;
    usernameLabel:cc.EditBox;
    passwordLabel:cc.EditBox;
    


    onLoad () {           
        this.usernameLabel = this.loginPanel.getChildByName("username").getComponent(cc.EditBox);
        this.passwordLabel = this.loginPanel.getChildByName("password").getComponent(cc.EditBox);
        this.roomLabel = this.inputRoomPanel.getChildByName("RoomID").getComponent(cc.Label);    
     
        //加载资源
        cc.loader.loadResDir("Texture");
     
        //清空数据
        QM.Room.Instance.clear();
        QM.User=null;

        var ws = new QM.Socket();        
        ws.connect();
        ws.on( this.message,this);


        this.regPanelBtn.on(cc.Node.EventType.TOUCH_START,()=>{
            this.regPanelToggle();
        },this);
      
    }

  
    message(ev:QM.QEvent){             
        //cc.log(ev);
        var type = ev.type;
        if(type=="open"){
            this.loadingNode.active = false;
            this.loginPanel.active = true;
            return false;
        }
        var cmd = ev.cmd;
        var data = ev.data;
        if(cmd && this[cmd]){
            this[cmd](data)
        }
     
    }

    

    error(msg:{msg:string}){    
        var errorNode = cc.instantiate(this.errorPrefab);  
        errorNode.parent = this.node;      
        errorNode.getComponent(ErrorManager).show(msg.msg);
    }

    regSuccess(){
        this.regPanel.active = false;
    }


    hideLoginPanel(){
        this.loginPanel.active = false;
    }

    hideInputRoomPanel(){
        this.inputRoomPanel.active = false;
    }

    /** 房间号输入盘 */
    inputNumber(e:cc.Event.EventTouch){
        var num = e.target.getChildByName("Label").getComponent(cc.Label).string;

        if(!isNaN(num)){            
            this.roomLabel.string+=num;
        }else if(num=="D"){
            this.roomLabel.string = "";
        }else if(num=="B"){
            this.roomLabel.string = this.roomLabel.string.slice(0,-1);
        }
        if(this.roomLabel.string.length==6){         
            var obj={
                c:"User",
                a:"joinRoom",
                d:{
                    roomId:this.roomLabel.string
                }
            }
            QM.Socket.Instance.Send(obj);
           
            this.roomLabel.string="";
        }
    }

    /** 点击登录按钮 */
    loginButton(){
        var username = this.usernameLabel.string;
        var password = this.passwordLabel.string;
        var obj={
            c:"User",
            a:"login",
            d:{
                username:username,
                password:password
            }
        }
        QM.Socket.Instance.Send(obj);
     
    }
    /** 接收服务端返回登录数据 */
    login(obj:{user:QM.Character}){        
        var user = obj.user;
        QM.User = user;
        this.loginPanel.active = false;
        this.inputRoomPanel.active = true;       
    }

    /** 接收服务端返回的房间信息 */
    roomUserInfo(obj:{rid:number,users:QM.Character[]}){
        var room = QM.Room.Instance;
        room.roomId = obj.rid;   
        obj.users.forEach(user => {
            room.addCharacter(user)
        });
        cc.director.loadScene("game"); 
    }


    /** 注册面板 显示隐藏 */
    regPanelToggle(){
        this.regPanel.active = !this.regPanel.active;
    }

    onDestroy(){
        QM.Socket.Instance.off(this.message,this);
    }
}
